Skip to content

fix: limit Numba threads in Wilcoxon path of rank_genes_groups#4082

Merged
flying-sheep merged 8 commits into
scverse:mainfrom
JhonatanFelix:main
Apr 24, 2026
Merged

fix: limit Numba threads in Wilcoxon path of rank_genes_groups#4082
flying-sheep merged 8 commits into
scverse:mainfrom
JhonatanFelix:main

Conversation

@JhonatanFelix
Copy link
Copy Markdown
Contributor

@JhonatanFelix JhonatanFelix commented Apr 23, 2026

Summary

This PR limits Numba thread usage during the wilcoxon path of scanpy.tl.rank_genes_groups by temporarily applying scanpy.settings.n_jobs via numba.set_num_threads(.....) and restoring the previous value afterward.

Implementation

  • added a private _numba_thread_limit(....) context manager in scanpy.tools._rank_genes_groups
  • used numba.get_num_threads() / numba.set_num_threads(...)
  • clamped the requested value to the valid range
  • restored the previous thread count in a finally block
  • applied the limit only when method == "wilcoxon"

Required checks

Added some focused tests for the new numba thread-limiting behavior in rank_genes_groups:

  • temporary restoration of the previous numba thread count
  • restoration on exception
  • clamping to the configured numba maximum
  • applying the thread limit for method="wilcoxon" using scanpy.settings.n_jobs
  • not applying the thread limit for unrelated methods
  • Release notes not necessary because: It is a small internal behavior fix, it does not change the public API or user workflow.

@JhonatanFelix JhonatanFelix marked this pull request as ready for review April 23, 2026 10:09
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.63%. Comparing base (44cfc6e) to head (6be0d40).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4082      +/-   ##
==========================================
+ Coverage   78.59%   78.63%   +0.03%     
==========================================
  Files         118      119       +1     
  Lines       12745    12762      +17     
==========================================
+ Hits        10017    10035      +18     
+ Misses       2728     2727       -1     
Flag Coverage Δ
hatch-test.low-vers 77.93% <100.00%> (+0.03%) ⬆️
hatch-test.pre 78.51% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/scanpy/_utils/__init__.py 75.11% <100.00%> (+0.05%) ⬆️
src/scanpy/_utils/_numba.py 100.00% <100.00%> (ø)
src/scanpy/tools/_rank_genes_groups.py 92.85% <100.00%> (+0.02%) ⬆️

... and 1 file with indirect coverage changes

@flying-sheep flying-sheep added this to the 1.12.2 milestone Apr 24, 2026
@flying-sheep
Copy link
Copy Markdown
Member

Looks great! Please merge JhonatanFelix#1

@JhonatanFelix
Copy link
Copy Markdown
Contributor Author

Done! @flying-sheep

@flying-sheep flying-sheep merged commit 2b60935 into scverse:main Apr 24, 2026
24 checks passed
@flying-sheep
Copy link
Copy Markdown
Member

wonderful, thank you!

flying-sheep pushed a commit that referenced this pull request Apr 24, 2026
@scverse scverse deleted a comment from lumberbot-app Bot Apr 24, 2026
flying-sheep added a commit that referenced this pull request Apr 24, 2026
…xon path of rank_genes_groups) (#4088)

Co-authored-by: Jhonatan Felix <108437587+JhonatanFelix@users.noreply.github.com>
flying-sheep added a commit that referenced this pull request Apr 26, 2026
Co-authored-by: Philipp A. <flying-sheep@web.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Call numba.set_num_threads when n_jobs is specified (e.g. in scanpy.tl.rank_genes_group)

2 participants